/******************************************************************************************************
Copyright (C) 2008 Mestrelab Research S.L. All rights reserved.

This file is part of the MNova scripting toolkit.

Authorized users of MNova Software may use this file freely, but this file is provided AS IS
with NO WARRANTY OF ANY KIND, INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE.
*****************************************************************************************************/

// This is an example for checking that mandatory fields have been filled in when saving to MnDB. 
// It can be used with layout templates containg named fields (see dbAddNamedFields.qs)
// To use: 
// 1. Place this script in one of the Mnova scripts folders.
// 2. Start saving to the new db. 
// 4. Customize the function dbPreSaveHook according to your needs.

// This function, when in the global namespace, is called when saving to database, after selecting the items 
// to be saved but before sending the data to the server.
// Parameters: aDBClient - DBClient object which is connected to the server we want to save to
//                   aDB - DBDefinition object for the database where items will be saved to
//             aRecordId - ID of the record to be updated, or 0 for a new record (Currently always 0)
//              aDBItems - Items to be saved under this record
function dbPreSaveHook(aDBClient, aDB, aRecordId, aDBItems, aShowErrors, aInteractive)
{
        if (!(aDB.hasField("Molecule", "Project")))
                return;

	var projectName = getNamedTextField("Field", "[ProjectName]")
	if (projectName == null || projectName == "-")
	{
		MessageBox.information("Error: Project information is not set. Aborting Save.");
		aDBItems.length = 0;	
	}
	else if (projectName.length != 10)
	{
		MessageBox.information("Error: Project information must be exactly 10 characters. Aborting Save.");
		aDBItems.length = 0;	
	}
}

